package com.ys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ys.dto.StuDto;
import com.ys.entity.Stu;
import com.ys.service.StuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName StuController
 * @Author 1628994918qq.com
 * @Date 2022/11/25 17:12
 */
@Slf4j
@RestController
public class StuController {
    @Autowired
    private StuService stuService;

    @RequestMapping("/selectAbs")
    public void selectAbs(){
        int i = stuService.selectAbs();
        System.out.println("i =" + i);
    }

    @RequestMapping("/getAllStu")
    public List<Stu> getAllStu(){
        log.info("============开始查询============");
        log.info("-----查询所有的学生信息-----");
        List<Stu> stu = stuService.getStu();
        //System.out.println("stu = " + stu);
        log.info("============查询结束============");
        return stu;
    }

    @RequestMapping("/getStuById")
    public Stu getStuById(Integer id){
        log.info("============开始查询============");
        log.info("-----通过学生id查询学生信息-----");
        Stu stu = stuService.getStuById(id);
        //System.out.println("stu = " + stu);
        log.info("============查询结束============");
        return stu;
    }

    @RequestMapping("/getStuByPage")
    public Page<Stu> getStuByPage(){
        /*int page =  1;
        int pageSize = 5;*/
        Page<Stu> rowPage  = new Page<>();
        //Page<Stu> rowPage = new Page(page, pageSize);
        rowPage.setSize(3);
        rowPage.setCurrent(1);
        Page<Stu> stuByPage = stuService.getStuByPage(rowPage);
        System.out.println("总条数 = " + stuByPage.getTotal());
        System.out.println("总页数 = " + stuByPage.getPages());
        System.out.println("当前页 = " + stuByPage.getCurrent());
        System.out.println("条数 = " + stuByPage.getSize());
        return stuByPage;
    }

    @RequestMapping("/getStuByIds")
    public List<Stu> getStuByIds(){
        log.info("============开始查询============");
        List<Integer> ids = new ArrayList<>();
        ids.add(13);
        ids.add(14);
        ids.add(15);
        ids.add(16);
        List<Stu> stus = stuService.selectStuByIds(ids);
        System.out.println("stus = " + stus);
        log.info("============查询结束============");
        return stus;
    }

    @RequestMapping("/getStuByMap")
    public List<Stu> getStuByMap(){
        log.info("============开始查询============");
        Map<String,Object> map = new HashMap<>();
        map.put("stu_name","闫帅");
        //map.put("stu_name","苏龙潮");
        map.put("stu_id","1710272310");
        List<Stu> stus = stuService.selectByMap(map);
        log.info("============查询结束============");
        return stus;
    }
    @RequestMapping("/getStuByMap1")
    public List<Stu> getStuByMap1(@RequestBody StuDto stuDto){
        log.info("============开始查询============");
        Map<String,Object> map = new HashMap<>();
        //map.put("id",stuDto.getId());
        map.put("stu_name",stuDto.getStuName());
        //map.put("stu_id",stuDto.getStuId());
        //map.put("stu_password",stuDto.getStuPassword());
        /*map.put("stu_name","闫帅");
        //map.put("stu_name","苏龙潮");
        map.put("stu_id","1710272310");*/
        List<Stu> stus = stuService.selectByMap(map);
        log.info("============查询结束============");
        return stus;
    }

    @RequestMapping("/getListStu")
    public List<Stu> getListStu(){
        log.info("============开始查询============");
        List<Stu> stus = stuService.selectList();
        log.info("============查询结束============");
        return stus;
    }

    @RequestMapping("/insertStuList")
    @Async
    @Transactional(rollbackFor = Exception.class)
    public String insertStuList() {
        log.info("============开始插入数据============");


        StuDto stuDto = new StuDto();
        stuDto.setStuId("123456789");
        stuDto.setStuName("zhangsan");
        stuDto.setStuPassword("san");
        log.info("线程-" + Thread.currentThread().getId() + "在执行写入");
        stuService.insertStus(stuDto);
        log.info("线程-" + Thread.currentThread().getId() + "执行完成");
        StuDto stuDto1 = new StuDto();
        stuDto1.setStuId("222222222");
        stuDto1.setStuName("lisi");
        stuDto1.setStuPassword("li");
        log.info("线程-" + Thread.currentThread().getId() + "在执行写入");
        stuService.insertStus(stuDto1);
        log.info("线程-" + Thread.currentThread().getId() + "执行完成");
        StuDto stuDto2 = new StuDto();
        stuDto2.setStuId("1111111111");
        stuDto2.setStuName("wangwu");
        stuDto2.setStuPassword("wang");
        log.info("线程-" + Thread.currentThread().getId() + "在执行写入");
        stuService.insertStus(stuDto2);
        log.info("线程-" + Thread.currentThread().getId() + "执行完成");
        log.info("============插入数据结束============");
        return "插入成功";
    }

    @RequestMapping("/testMap")
    public String testMap(){
        Map<String,Object> map = new HashMap<>();
        Stu stu = new Stu();
        stu.setStuId("11122233");
        stu.setStuName("yyyyy");
        stu.setStuPassword("1234567891");
        map.put("id","111222333");
        map.put("name","yanshuai");
        map.put("password","a");
        map.put("stu",stu);
        int i = stuService.insertStu(map);
        return "成功";
    }

    @RequestMapping("/testString")
    public void testString(){
        int id = 1061416962;
        Stu stu = stuService.update111(id);
        System.out.println(stu);
    }
    @RequestMapping("/testMap1")
    public void testMap1(){
        Map<String, Object> map = stuService.selectAll();
        System.out.println(map);
        for (Map.Entry<String,Object> entry:map.entrySet()) {
            System.out.println(entry.getKey()+"----"+entry.getValue());
        }
    }
    @RequestMapping("/testListMap")
    public void testListMap(){
        List<Map<String, Object>> map = stuService.selectListAll();
        for (int i = 0; i <map.size(); i++) {
            System.out.println("每一个对象"+map.get(i));
        }
    }

    @RequestMapping("/testFile")
    public void testFile(){
        List<File> files = new ArrayList<>();
        File file = new File("D:\\软考.zip");
        File file1 = new File("D:\\新xq建文件夹.7z");
        files.add(file);
        files.add(file1);
        for (int i = 0; i < files.size(); i++) {
            System.out.println("aaaa----" + files.get(i));
        }
    };
}
